package com.idroid.activities;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.idroid.R;
import com.idroid.dialogs.HelpDialog;
import com.idroid.domain.LocationTracker;
import com.idroid.domain.ParkingArea;
import com.idroid.domain.SerializableLocation;
import com.idroid.domain.States;

/**
 * Displays a list of contacts.
 * 
 */
public class ParkingDetailActivity extends Activity {
	public static final String LOG_TAG = "DetailActivity";

	private TextView _phone;
	private TextView _location;
	private TextView _name;
	private TextView _address;
	private TextView _type;
	private TextView _distanceFromUser;
	private TextView _rate;
	private TextView _paymentMethod;
	private TextView _size;
	private TextView _availability;
	private SerializableLocation _startingLoc;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.detail_item);

		ToolbarConfig toolbarConfig = new ToolbarConfig(this, "Area Details");

		Button helpButton = toolbarConfig.getToolbarHelpButton();
		helpButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				HelpDialog dialog = HelpDialog
						.newInstance(
								ParkingDetailActivity.this,
								"Parking Detail Help",
								"This page shows all of the details about a specific Parking Area.  Clicking display will return the user to the map view, with that parking area selected.  If the distance is set to N/A please retry.");
				dialog.show(ParkingDetailActivity.this.getFragmentManager(),
						"parkinglisthelp");

			}
		});

		Bundle extra = getIntent().getExtras();
		// _whClicked = extra.getSerializable("Value").toString();

		_address = (TextView) findViewById(R.id.item_address);
		_type = (TextView) findViewById(R.id.item_type);
		_distanceFromUser = (TextView) findViewById(R.id.item_distance);
		_paymentMethod = (TextView) findViewById(R.id.item_paymentMethod);
		_rate = (TextView) findViewById(R.id.item_rate);
		_size = (TextView) findViewById(R.id.lot_size);
		_availability = (TextView) findViewById(R.id.lot_availability);
		_phone = (TextView) findViewById(R.id.item_phone);
		_location = (TextView) findViewById(R.id.item_location);
		_name = (TextView) findViewById(R.id.item_areaName);

		final ParkingArea area = (ParkingArea) extra
				.getSerializable("parkingArea");

		Button displayButton = (Button) findViewById(R.id.toolbar_display_button);
		displayButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent i = new Intent(ParkingDetailActivity.this,
						FragmentTabsActivity.class);

				i.putExtra("focusedArea", area);

				i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				// i.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
				setResult(1, i);
				finish();

			}
		});
		

		_startingLoc = (SerializableLocation) extra.getSerializable("startingLoc");

		Button googleMapsButton = (Button) findViewById(R.id.toolbar_google_maps_button);
		googleMapsButton.setOnClickListener(new OnClickListener() {


			public void onClick(View v) {

				Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
						Uri.parse("http://maps.google.com/maps?saddr="
								+ _startingLoc.getLatitude() + ","
								+ _startingLoc.getLongitude() + "&daddr="
								+ area.getLatitude() + ","
								+ area.getLongitude()));

				intent.setClassName("com.google.android.apps.maps",
						"com.google.android.maps.MapsActivity");
				startActivity(intent);

			}
		});

		_name.setText(area.getAreaName());
		_address.setText(area.getAddress());
		_location.setText(area.getCity() + ", "
				+ States.GetStateByName(area.getState()) + " "
				+ area.getZipCode());
		_phone.setText(area.getPhoneNumber());
		_type.setText(area.getAreaType().getDescription());

		if (Float.compare(area.getDistanceFromUserInMiles(), Float.MAX_VALUE) == 0) {
			_distanceFromUser.setText("N/A");
		} else {
			DecimalFormat myFormatter = new DecimalFormat("###,###,###.###");
			String output = myFormatter.format(area
					.getDistanceFromUserInMiles());
			_distanceFromUser.setText(output + " Miles");
		}

		_rate.setText(area.getFormattedHourlyRate());
		_paymentMethod.setText(area.getPaymentMethod().getDescription());
		_size.setText(area.getNumberOfSpaces() + "");
		_availability.setText(area.getSpacesAvailable() + "");

		Intent i = new Intent();
		i.putExtra("showsearch", true);

		this.setResult(1, i);

	}
}
